#include "Territoire.hpp"

using namespace std;
using namespace sf;

Territoire::Territoire()
{
    //ctor
}

void Territoire::ajouterHexagone(Hexagone &h,vector<Hexagone> &v)
{
    int premierCommun(-1),dernierCommun(-1),pointAjoute;

    int max(surface.getPointCount());

    int pointCourant(0);

    while (pointCourant<max)
    {
        int pointCourantAjoute(0);

        while ((pointCourantAjoute<6)&&(surface.getPoint(pointCourant)!=h.getPoint(pointCourantAjoute)))
        {
            pointCourantAjoute++;
        }
        if ((pointCourantAjoute<6)&&(surface.getPoint(pointCourant)==h.getPoint(pointCourantAjoute)))
        {
            if (premierCommun==-1)
            {
                premierCommun=pointCourant;
                pointAjoute=pointCourantAjoute;
            }
            else if (dernierCommun<pointCourant)
            {
                dernierCommun=pointCourant;
            }
        }
        pointCourant++;
    }

    if (premierCommun==-1)
    {
        v.push_back(h);
    }
    else
    {
        int diff(6+(2*premierCommun)-(2*dernierCommun));

        surface.setPointCount(surface.getPointCount()+diff);

        for (int i(max-1);i>(dernierCommun-1);i--)
        {
            surface.setPoint(i+diff,surface.getPoint(i));
        }

        for (int i(premierCommun+1);i<(dernierCommun+diff);i++)
        {
            pointAjoute=(pointAjoute+1)%6;
            surface.setPoint(i,h.getPoint(pointAjoute));
        }
    }
}

void Territoire::creerFrontiere()
{
    int nbPoints(surface.getPointCount());

    VertexArray f(LinesStrip,(nbPoints+1));

    for (int i(0);i<nbPoints;i++)
    {
        Vertex v(surface.getPoint(i),Color::Black);
        f[i]=v;
    }
    Vertex v(surface.getPoint(0),Color::Black);
    f[nbPoints]=v;

    frontiere=f;
}

Territoire::~Territoire()
{
    //dtor
}
